<template>
  <div class="whole">
    <el-container>
      <el-header>
        <div><b style="float: left;margin-top: -20px"><h2 style="color: #d79797">网上花店商城</h2></b>
          <el-button class="logout" type="warning" @click="logout">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            router
            :default-active="defaultUrl"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#d79797"
          >
            <el-menu-item index="index">
              <i class="el-icon-s-home"></i>
              首页
            </el-menu-item>


            <el-menu-item index="goods">
              <i class="el-icon-menu"></i>
              商品列表
            </el-menu-item>

            <el-menu-item index="cart">
              <i class="el-icon-document"></i>
              <span>购物车</span>
            </el-menu-item>

            <el-menu-item index="order">
              <i class="el-icon-document"></i>
              <span>我的订单</span>
            </el-menu-item>

            <el-menu-item index="updateInfo">
              <i class="el-icon-document"></i>
              <span>个人信息</span>
            </el-menu-item>

          </el-menu>
        </el-aside>
        <el-container>
          <!--     主体区域     -->
          <el-main>
            <!-- 公告组件 -->
            <div class="announcement-section" v-if="$route.path === '/goods'">
              <el-card class="announcement-card">
                <div slot="header" class="clearfix">
                  <span style="font-weight: bold; color: #d79797;">
                    <i class="el-icon-bell"></i> 最新公告
                  </span>
                </div>
                <div class="announcement-content">
                  <div v-if="announcements.length === 0" class="no-announcement">
                    暂无公告
                  </div>
                  <div v-else>
                    <div v-for="(announcement, index) in announcements" :key="announcement.id" 
                         class="announcement-item" :class="{ 'active': index === 0 }">
                      <div class="announcement-title">
                        <i class="el-icon-message"></i>
                        {{ announcement.title }}
                      </div>
                      <div class="announcement-text">{{ announcement.content }}</div>
                      <div class="announcement-time">{{ formatTime(announcement.createTime) }}</div>
                    </div>
                  </div>
                </div>
              </el-card>
            </div>
            <router-view></router-view>
          </el-main>
        </el-container>
      </el-container>

    </el-container>


  </div>
</template>

<script>
  export default {
    data() {
      return {
        activeIndex: 1,
        defaultUrl: 'index', // 修改默认高亮为首页
        announcements: [],
      }
    },
    // 监听路由变化
    watch: {
      '$route': 'getPath'
    },
    mounted() {
      this.getAnnouncements();
      this.getPath(); // 保证初始进入时也能正确高亮
    },
    methods: {
      getPath() {
        // 修正逻辑：/user 或 /user/index 都高亮首页
        const path = this.$route.path.replace('/user/', '');
        if (path === '' || path === 'index') {
          this.defaultUrl = 'index';
        } else if (path.startsWith('goods')) {
          this.defaultUrl = 'goods';
        } else if (path.startsWith('cart')) {
          this.defaultUrl = 'cart';
        } else if (path.startsWith('order')) {
          this.defaultUrl = 'order';
        } else if (path.startsWith('updateInfo')) {
          this.defaultUrl = 'updateInfo';
        } else {
          this.defaultUrl = '';
        }
      },
      changePage(path) {
        this.$router.push(path)
      },
      logout() {
        this.$router.push('/login');
      },
      // 获取公告列表
      getAnnouncements() {
        this.$http.get('/announcement/list').then(result => {
          let R = result.data;
          if (R.code === 2000) {
            this.announcements = R.data.slice(0, 3); // 只显示最新的3条公告
          }
        }).catch(error => {
          console.error('获取公告失败:', error);
        });
      },
      // 格式化时间
      formatTime(time) {
        if (!time) return '';
        const date = new Date(time);
        return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN', {hour12: false});
      },
    },
  }
</script>

<style scoped>
  .whole {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }


  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-menu {
    border-right: none;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 20px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container .el-aside {
    line-height: 320px;
  }

  .my-menu {
    margin: 10px;
  }

  .main {
    height: 80%;
  }

  .el-container {
    height: 100%;
  }

  .el-header {
    background-color: #ffffff;
    padding: 0 20px;
    height: 60px !important;
    line-height: 60px;
    border-bottom: 1px solid #e6e6e6;
  }

  .el-aside {
    background-color: #545c64;
    width: 200px;
    overflow-x: hidden;
    overflow-y: auto;
  }

  .el-main {
    background-color: #f5f5f5;
    padding: 20px;
    overflow-y: auto;
    height: calc(100vh - 60px);
  }

  .menu-child-item {
    margin-left: 20px;
  }

  .el-footer {
    background-color: cornflowerblue;
  }

  .welcome {
    float: left;

    color: cornflowerblue;
  }

  /* 公告组件样式 */
  .announcement-section {
    margin-bottom: 20px;
  }

  .announcement-card {
    margin-bottom: 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  }

  .announcement-content {
    max-height: 300px;
    overflow-y: auto;
  }

  .announcement-item {
    padding: 15px;
    border-bottom: 1px solid #f0f0f0;
    transition: all 0.3s ease;
  }

  .announcement-item:last-child {
    border-bottom: none;
  }

  .announcement-item:hover {
    background-color: #f8f9fa;
  }

  .announcement-item.active {
    background-color: #fff3cd;
    border-left: 4px solid #d79797;
  }

  .announcement-title {
    font-weight: bold;
    color: #333;
    margin-bottom: 8px;
    font-size: 14px;
  }

  .announcement-title i {
    color: #d79797;
    margin-right: 5px;
  }

  .announcement-text {
    color: #666;
    font-size: 13px;
    line-height: 1.5;
    margin-bottom: 8px;
    word-break: break-all;
  }

  .announcement-time {
    color: #999;
    font-size: 12px;
    text-align: right;
  }

  .no-announcement {
    text-align: center;
    color: #999;
    padding: 30px;
    font-style: italic;
  }
</style>
